package com.swind.fast.modules.app.railway.project.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.util.CommonUtils;
import com.swind.fast.common.util.MinioUtil;

// import com.swind.fast.modules.app.railway.project.vo.RailwayProjectFileVo;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectFile;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectFileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/app/railway/project/file")
@Slf4j
public class RailwayProjectFileController {

    @Autowired
    private IRailwayProjectFileService railwayProjectService;

    @GetMapping(value = "/list")
    public Result<?> queryPageList(RailwayProjectFile railwayProject,
                                      @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                      @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                      HttpServletRequest req) {

        QueryWrapper<RailwayProjectFile> queryWrapper = QueryGenerator.initQueryWrapper(railwayProject, req.getParameterMap());
        Page<RailwayProjectFile> page = new Page<RailwayProjectFile>(pageNo, pageSize);
        IPage<RailwayProjectFile> pageList = railwayProjectService.page(page, queryWrapper);

        // IPage<RailwayProjectFileVo> rpageList = railwayProjectService.getFileListPage(page, queryWrapper);

        // pageList.getRecords().stream().forEach(item -> {
        //     item.setFileUrl(MinioUtil.getMinioFileUrl(item.getFileUrl()));
        // });

        return Result.OK(pageList);
    }

    @PostMapping(value = "/minio/upload")
    public Result<?> minioUploadAction(HttpServletRequest request) {
        String bizPath = request.getParameter("biz");
        if (oConvertUtils.isEmpty(bizPath)){
            bizPath = "file";
        }

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
        String orgName = file.getOriginalFilename();// 获取文件名
        orgName = CommonUtils.getFileName(orgName);
        String fileUrl = MinioUtil.upload(file, bizPath);
        if (oConvertUtils.isEmpty(fileUrl)) {
            return Result.error("上传失败,请检查配置信息是否正确!");
        }
        String fileName = request.getParameter("fileName");
        String projectId = request.getParameter("projectId");
        log.info("fileName: {}", fileName);
        // String fileName = multipartRequest.get
        //保存文件信息
        RailwayProjectFile minioFile = new RailwayProjectFile();
        minioFile.setFileName(fileName);
        minioFile.setOssName(orgName);
        minioFile.setFileUrl(fileUrl);
        minioFile.setProjectId(projectId);
        minioFile.setCreateTime(new Date());
        railwayProjectService.save(minioFile);

        return Result.OK("上传成功！");
    }

    // @AutoLog(value = "fileUrl")
    // @ApiOperation(value="fileUrl", notes="fileUrl")
    @GetMapping(value = "/access/url")
    public Result<?> getAccessUrl(@RequestParam(name="id", required=true) String id, @RequestParam(name="period", defaultValue="1") Integer period) {
        // RailwayProjectFile file = railwayProjectService.getById(id);
        // String fileHttpUrl = file.getFileUrl();
        // String objectName = MinioUtil.getObjectNameByUrl(fileHttpUrl);

        // Integer day = 60 * 60 * 24 * period;
        // log.info("id: {}, period: {}", id, period);

        // String fileUrl = MinioUtil.getObjectURL(objectName, day);
        // return Result.OK(fileUrl);
        return Result.OK("成功！");
    }

    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody RailwayProjectFile railwayProject) {
        railwayProjectService.save(railwayProject);
        return Result.OK("添加成功！");
    }

    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody RailwayProjectFile railwayProject) {
        railwayProjectService.updateById(railwayProject);
        return Result.OK("编辑成功!");
    }

    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        railwayProjectService.removeById(id);
        return Result.OK("删除成功!");
    }
}
